package com.xq.shop.controller.user;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xq.shop.po.Point;
import com.xq.shop.po.PointRecord;
import com.xq.shop.po.User;
import com.xq.shop.service.user.IPointService;
import com.xq.shop.util.Const;
import com.xq.shop.util.GsonUtil;
import com.xq.shop.util.UserServiceFactory;
import com.xq.shop.vo.SearchObject;
import com.xq.shop.vo.SearchValue.Pattern;

@Controller
@RequestMapping("/user/")
public class PointCtrl extends BaseCtrl<Point> {

	private IPointService pointService = UserServiceFactory.getPointService();
	
	@Override
	public IPointService getService() {
		return pointService;
	}

	@RequestMapping("point_list.do")
	@ResponseBody
	public String list(SearchObject search, HttpServletRequest request) {
		
		User user = (User) request.getSession().getAttribute(Const.User_Session);
		search.addCondition("user_id", user.getId(), Pattern.EQUAL);
		return super.list(search, request);
	}
	
	@RequestMapping("point_record_list.do")
	@ResponseBody
	public String recordList(SearchObject search, HttpServletRequest request) {
		
		User user = (User) request.getSession().getAttribute(Const.User_Session);
		search.addCondition("user_id", user.getId(), Pattern.EQUAL);
		return GsonUtil.toJson(pointService.getRecord(search));
	}
	
	@RequestMapping("point_add.do")
	public void addPoint(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute(Const.User_Session);
		try {
			pointService.addPoint(user.getId(), 14, PointRecord.GRANT, null, "用户消费120元");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
